/*
 * Copyright (c) 2010-2011 Josef Hardi <josef.hardi@gmail.com>
 * Copyright (c) 2010 Piotr Zagawa <piotr.zagawa@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package id.duapuluhdua.dakidaki.common;

import android.database.Cursor;

import id.duapuluhdua.dakidaki.Request;
import id.duapuluhdua.dakidaki.UserAlert;
import id.duapuluhdua.dakidaki.data.common.DataTable;
import id.duapuluhdua.dakidaki.data.common.DataValues;

/**
 *
 */
public class CommonActivityTable
{
  /**
   * Data transfer interface from and to this controllers
   */
  public interface DataControlsTransfer
  {
    /**
     * Implements the reading values from UI input objects (e.g., edit text or
     * check box) and prepare them for table row update.
     *
     * @param table
     *          The table object from which we obtain the the {@link DataValues}
     *          object.
     * @return The {@link DataValues} that contains the initial input
     *         information.
     */
    public DataValues getControlValuesForUpdate(DataTable table);

    /**
     * Implements the displaying values to the UI input object (e.g., edit text
     * of check box) from the current table row for viewing or editing purposes.
     *
     * @param values
     *          The obtained data values from the current table row.
     */
    public void setControlValuesForView(DataValues values);
  }

  private CommonActivity mParent;
  private DataTable mDataTable;
  private Request mData;

  /**
   * The constructor.
   *
   * @param parent
   */
  public CommonActivityTable(CommonActivity parent)
  {
    mParent = parent;
  }

  /**
   *
   * @param table
   * @param data
   */
  public void setData(DataTable table, Request data)
  {
    mDataTable = table;
    mData = data;
  }

  /**
   *
   * @param table
   * @return
   */
  private DataValues getValuesForOperation(DataTable table)
  {
    try {
      final DataValues values = mParent.getControlValuesForUpdate(table);
      if (values == null) {
        UserAlert.show(mParent, UserAlert.Result.DATA_VALUES_ERROR);
      }
      return values;
    }
    catch (Exception e) {
      UserAlert.show(mParent, UserAlert.Result.WRONG_VALUES);
    }
    return null;
  }

  /**
   *
   * @return
   */
  public long getRowIdForOperation()
  {
    final long lRowId = mData.getRowId();
    if (lRowId == -1) {
      UserAlert.show(mParent, UserAlert.Result.MORE_DATA_REQUIRED);
    }
    return lRowId;
  }

  /**
   *
   * @return
   */
  public long dataInsert()
  {
    final DataTable table = mDataTable;

    if (table != null) {
      final DataValues values = getValuesForOperation(table);
      if (values != null) {
        if (values.valuesComplete()) {
          final UserAlert.Result validationResult = table.validateValues(values);
          if (validationResult == null) { // all OK
            final long lRowID = table.dataInsert(values);
            if (lRowID == -1) {
              UserAlert.show(mParent, UserAlert.Result.DATA_INSERT_ERROR);
            }
            else {
              return lRowID;
            }
          }
          else {
            UserAlert.show(mParent, validationResult);
          }
        }
        else {
          UserAlert.show(mParent, UserAlert.Result.ENTER_ALL_DATA);
        }
      }
    }
    return -1;
  }

  public boolean dataUpdate()
  {
    final DataTable table = mDataTable;

    if (table != null) {
      final DataValues values = getValuesForOperation(table);
      if (values != null) {
        final long lRowId = getRowIdForOperation();
        if (lRowId != -1) {
          if (values.valuesComplete()) {
            final UserAlert.Result validationResult = table.validateValues(values);
            if (validationResult == null) {
              if (table.dataUpdate(values, lRowId)) {
                return true;
              }
              else {
                UserAlert.show(mParent,
                    UserAlert.Result.DATA_UPDATE_ERROR);
              }
            }
            else {
              UserAlert.show(mParent, validationResult);
            }
          }
          else {
            UserAlert.show(mParent, UserAlert.Result.ENTER_ALL_DATA);
          }
        }
      }
    }
    return false;
  }

  public boolean getDataForView()
  {
    final DataTable table = mDataTable;

    if (table != null) {
      final long lRowId = getRowIdForOperation();
      if (lRowId != -1) {
        final Cursor inputCursor = table.getDataRecord(lRowId);
        if (inputCursor == null) {
          UserAlert.show(mParent, UserAlert.Result.DATA_VALUES_ERROR);
          return false;
        }
        final DataValues values = new DataValues(table, inputCursor);
        mParent.setControlValuesForView(values);

        return true;
      }
    }
    return false;
  }
}
